#include "threshold_plugin.h"

#include "ui_plugin.h"

#define THRESHOLD_PAGE                      0
#define ADAPTIVETHRESHOLD_PAGE              1

Threshold_Plugin::Threshold_Plugin()
{
    // Insert initialization codes here ...
}

Threshold_Plugin::~Threshold_Plugin()
{
    // Insert cleanup codes here ...
}

QString Threshold_Plugin::title()
{
    return this->metaObject()->className();
}

QString Threshold_Plugin::version()
{
    return "1.0.0";
}

QString Threshold_Plugin::description()
{
    return "";
}

QString Threshold_Plugin::help()
{
    return "";
}

void Threshold_Plugin::setupUi(QWidget *parent)
{
    ui = new Ui::PluginGui;
    ui->setupUi(parent);

    ui->tabWidget->setCurrentIndex(0);


    ui->adapMethComboBox->addItems(
                QStringList()
                << "ADAPTIVE_THRESH_MEAN_C"
                << "ADAPTIVE_THRESH_GAUSSIAN_C");

    ui->adapTypeComboBox->addItems(
                QStringList()
                << "THRESH_BINARY"
                << "THRESH_BINARY_INV");

    ui->threshTypeCombo->addItems(
                QStringList()
                << "THRESH_BINARY"
                << "THRESH_BINARY_INV"
                << "THRESH_TRUNC"
                << "THRESH_TOZERO"
                << "THRESH_TOZERO_INV"
                << "THRESH_MASK"
                << "THRESH_OTSU"
                << "THRESH_TRIANGLE");

    connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(on_tabWidget_currentChanged(int)));

    connect(ui->threshTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_threshTypeCombo_currentIndexChanged(int)));
    connect(ui->threshSlider, SIGNAL(valueChanged(int)), this, SLOT(on_threshSlider_valueChanged(int)));
    connect(ui->threshMaxSlider, SIGNAL(valueChanged(int)), this, SLOT(on_threshMaxSlider_valueChanged(int)));

    connect(ui->adapTypeComboBox,SIGNAL(currentIndexChanged(int)), this, SLOT(on_adapTypeComboBox_currentIndexChanged(int)));
    connect(ui->adapMethComboBox,SIGNAL(currentIndexChanged(int)), this, SLOT(on_adapMethComboBox_currentIndexChanged(int)));
    connect(ui->adapMaxSlider, SIGNAL(valueChanged(int)), this, SLOT(on_adapMaxSlider_valueChanged(int)));
    connect(ui->blockSpinBox,SIGNAL(valueChanged(int)),this,SLOT(on_blockSpinBox_valueChanged(int)));

    connect(ui->urlLabel,SIGNAL(linkActivated(QString)),this,SLOT(openURL(QString)));
}

void Threshold_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
{
    using namespace cv;

    Mat grayScale;
    cvtColor(inputImage, grayScale, COLOR_BGR2GRAY);

    switch(ui->tabWidget->currentIndex())
    {
        case THRESHOLD_PAGE:
            threshold(grayScale,
                      grayScale,
                      ui->threshSlider->value(),
                      ui->threshMaxSlider->value(),
                      ui->threshTypeCombo->currentIndex());

        break;

        case ADAPTIVETHRESHOLD_PAGE:
            adaptiveThreshold(grayScale,
                              grayScale,
                              ui->adapMaxSlider->value(),
                              ui->adapMethComboBox->currentIndex(),
                              ui->adapTypeComboBox->currentIndex(),
                              ui->blockSpinBox->value(),
                              0);

        break;

        default:
            break;
    }
    cvtColor(grayScale, outputImage, COLOR_GRAY2BGR);
}

void Threshold_Plugin::on_threshTypeCombo_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    emit updateNeeded();
}

void Threshold_Plugin::on_threshSlider_valueChanged(int value)
{
    emit infoMessage(QString::number(value));
    ui->threshLabel->setText(QString::number(value));
    emit updateNeeded();
}

void Threshold_Plugin::on_threshMaxSlider_valueChanged(int value)
{
    emit infoMessage(QString::number(value));
    ui->maxLabel->setText(QString::number(value));
    emit updateNeeded();
}

void Threshold_Plugin::on_adapTypeComboBox_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    emit updateNeeded();
}

void Threshold_Plugin::on_adapMethComboBox_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    emit updateNeeded();
}

void Threshold_Plugin::on_adapMaxSlider_valueChanged(int value)
{
    emit infoMessage(QString::number(value));
    ui->adapMaxLabel->setText(QString::number(value));
    emit updateNeeded();
}

void Threshold_Plugin::on_blockSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}


void Threshold_Plugin::on_tabWidget_currentChanged(int index)
{
    Q_UNUSED(index);
    emit updateNeeded();
}

void Threshold_Plugin::openURL(QString url)
{
    QDesktopServices::openUrl(url);
}
